package com.terren.spider.core.html.pipeline;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.terren.spider.core.html.configuration.Configuration;
import com.terren.spider.core.html.util.HtmlParseUtil;
import com.terren.spider.entity.config.SysConfig;
import com.terren.spider.util.common.LogUtil;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.CollectorPipeline;

public class HtmlConsolePipeline <T> implements CollectorPipeline<ResultItems> {
	protected List<ResultItems> collector = new ArrayList<ResultItems>();
	protected Logger log = LogUtil.getLogger(getClass());
	protected Class<T> clazz;
	protected HtmlParseUtil<T> parseUtil;//分析util
	protected Configuration configuration;
	protected Integer currentItem;// 当前页
	
	
	
	
	public HtmlConsolePipeline(Configuration configuration,Class<T> clazz ) throws Exception{
		super();
		this.configuration = configuration;
		this.clazz = clazz;
		this.parseUtil = new HtmlParseUtil<T>(configuration);
		currentItem = 0;
	}

	public HtmlConsolePipeline() {
		super();
	}

	@Override
	public void process(ResultItems resultItems, Task task) {
		if (resultItems.get(SysConfig.COLLECT_ITEM_KEY) instanceof Page) {
			Page page = resultItems.get(SysConfig.COLLECT_ITEM_KEY);
			Integer totalPage = resultItems.get(SysConfig.COLLECT_TOTAL_TARGET_URL);
			try {
				T pojo = parseUtil.parse(page,configuration.getEntry(), clazz);
				
				
				log.info("[total page:"+totalPage+"][current item:" + (++currentItem) + "]"+pojo.toString());
			} catch (Exception e) {
				log.error("HtmlConsolePipeline error",e);
			}
		}
		
	}

	@Override
	public List<ResultItems> getCollected() {
		// TODO Auto-generated method stub
		return collector;
	}


	public Class<T> getClazz() {
		return clazz;
	}

	public void setClazz(Class<T> clazz) {
		this.clazz = clazz;
	}

	public HtmlParseUtil<T> getParseUtil() {
		return parseUtil;
	}

	public void setParseUtil(HtmlParseUtil<T> parseUtil) {
		this.parseUtil = parseUtil;
	}

	public Configuration getConfiguration() {
		return configuration;
	}

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	} 

}
